<?php
namespace app\api\model;

use think\Model;
use think\Db;
use think\Session;

/**
 * www.zhouchunyu.com.
 * Created on 2019/1/7 17:22
 * Created by One <admin@zhouchunyu.com>
 */

class File extends Model {

    public $UID;

    public function __construct($uid = 0)
    {
        parent::__construct();
        $this->UID = $uid;
    }

    /**
     * 获取指定文件夹下的文件数量，不包含子文件夹
     * @param int $dir_id
     * @return int|string
     * @throws \think\Exception
     */
    public function getCount($folder_id=0, $show_del=false, $show_conceal=false) {
        $Obj = Db::name('files')->where('dir_id', $folder_id);
        if(!$show_del) {
            $Obj->where('is_del',0);
        }
        if(!$show_conceal) {
            $Obj->where('is_conceal',0);
        }
        return $Obj->count();
    }

    /**
     * 获取指定文件夹下的文件列表，不包含子文件夹
     * @param int $folder_id
     * @param int $page
     * @param int $limit
     * @param bool $isdel 是否查询已经删除的文件夹
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($folder_id=0, $page=1, $limit=50, $isdel=false) {
        $OBJ = Db::name('files')->where('dir_id', $folder_id)->page($page)->limit($limit);
        if($isdel) {
            $OBJ->where('is_del', 1)->order('del_time desc');
        } else {
            $OBJ->order('update_time desc');
        }

        $datas = $OBJ->select();

        foreach ($datas as &$data) {
            unset($data['client_ipaddr']);
            unset($data['uid']);
            $data['id'] = $data['fileid'];
            $data['type'] = 'file';
        }

        return $datas;
    }

    /**
     * 根据md5获取文件
     * @param string $md5
     */
    public function getFileByMd5($md5='') {
        return Db::name('files')->where('file_md5', $md5)->find();
    }

    /**
     * 根据文件名和路径获取指定文件，多数用于判断该目录是否有重命名文件
     * @param int $dirId
     * @param string $fileName
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getFileByDirAndFileName($dirId=0, $fileName='') {
        return Db::name('files')
            ->where('file_name', $fileName)
            ->where('dir_id', $dirId)
            ->where('uid', $this->UID)
            ->find();
    }

    /**
     * 根据文件md5和路径获取指定文件，多数用于判断该目录是否有重命名文件
     * @param int $dirId
     * @param string $md5
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getFileByDirAndMd5($dirId=0, $md5='') {
        return Db::name('files')
            ->where('file_md5', $md5)
            ->where('dir_id', $dirId)
            ->where('uid', $this->UID)
            ->find();
    }

    /**
     * 保存文件到数据库
     * @param $data
     * @return string
     */
    public function SaveFile($data) {
        Db::name('files')->insert($data);
        return Db::name('files')->getLastInsID();
    }

    /**
     * 获取用户容量大小
     * @param $uid
     * @return float|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getTotalStorage($uid){
        $userData = Db::name('users')->where('id',$uid)->find();
        $basicStronge = Db::name('groups')->where('id',$userData['user_group'])->find();

        if($basicStronge == 0) {
            return 1099511627776000000; // 100万TB
        }
        $addOnStorage = Db::name('storage_pack')
            ->where('uid',$uid)
            ->where('dlay_time',">",time())
            ->sum('pack_size');
        return $addOnStorage+$basicStronge["max_storage"];
    }

    /**
     * 获取用户已用容量
     * @param $uid
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUsedStorage($uid){
        $userData = Db::name('users')->where('id',$uid)->find();
        return $userData['used_storage'];
    }

    public function sotrageCheck($uid,$fsize){
        $totalStorage = $this->getTotalStorage($uid);
        $usedStorage = $this->getUsedStorage($uid);
        return ($totalStorage >= ($usedStorage + $fsize)) ? true : false;
    }

    /**
     * 设置用户已用空间
     * @param $uid
     * @param $size
     * @throws \think\Exception
     */
    public function storageCheckOut($uid,$size){
        Db::name('users')->where('id',$uid)->setInc('used_storage',$size);
    }
}